package com.zmwx.auth.application.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zmwx.auth.application.convert.AuthPermissionDTOToConverter;
import com.zmwx.auth.application.convert.AuthRoleDTOToConverter;
import com.zmwx.auth.application.dto.AuthPermissionDTO;
import com.zmwx.auth.application.dto.AuthRoleDTO;
import com.zmwx.auth.common.entity.Result;
import com.zmwx.auth.domain.entityDO.AuthPermissionBO;
import com.zmwx.auth.domain.entityDO.AuthRoleBO;
import com.zmwx.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

@Resource
    private AuthPermissionDomainService authPermissionDomainService;
    @RequestMapping("/add")
    public Result<Boolean> addPermission(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
              log.info("addPermission:{}", authPermissionDTO);
            }
            Preconditions.checkNotNull(authPermissionDTO.getName(), "权限名称不能为空" +
                    "");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "父ID 不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getPermissionKey(), "权限KEY 不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOToConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            boolean result = authPermissionDomainService.addPermission(authPermissionBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("新增权限失败error:{}", e.getMessage(), e);
            return Result.fail("新增权限失败");
        }
    }
    /**
     * 修改权限/更新权限
     */
    @RequestMapping("/update")
    public Result<Boolean> updatePermission(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getName(), "权限名称不能为空" +
                    "");
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOToConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            boolean result = authPermissionDomainService.updatePermission(authPermissionBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新权限信息失败");
        }
    }
        /**
         * 删除权限
         */
        @RequestMapping("delete")
        public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
            try {
                if (log.isInfoEnabled()) {
                    log.info("PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
                }
                Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
                AuthPermissionBO permissionBO = AuthPermissionDTOToConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
                return Result.ok(authPermissionDomainService.deletePermission(permissionBO));
            } catch (Exception e) {
                log.error("PermissionController.delete.error:{}", e.getMessage(), e);
                return Result.fail("删除权限信息失败");
            }
        }
    /**
     * 查询用户权限
     */
    @RequestMapping("/getPermission")
    public Result<Boolean> getPermission(String userName) {
        try {
            log.info("PermissionController.getPermission.userName:{}",userName);
            Preconditions.checkArgument(!StringUtils.isBlank(userName), "用户id不能为空");
            return Result.ok(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("PermissionController.getPermission.error:{}", e.getMessage(), e);
            return Result.fail("查询用户权限信息失败");
        }
    }
    }

